from django.shortcuts import render,redirect
from django.http import JsonResponse, HttpResponse
from book.models import *
import json
from django.db.models import F, Q, Count, Avg, Max, Min, Sum


# Create your views here.


def index(request):
    # BookInfo.objects.filter(pub_date__year=1980)
    # BookInfo.objects.filter(pub_date__gt='1990-1-1')
    # BookInfo.objects.filter(read_count__gt=F('comment_count'))
    # BookInfo.objects.filter(read_count__gt=F('comment_count')*2)
    # BookInfo.objects.filter(read_count__gt=20).filter(id__lt=3)
    # BookInfo.objects.filter(read_count__gt=20, id__lt=3)
    # BookInfo.objects.filter(Q(read_count__gt=20) | Q(id__lt=3))
    # BookInfo.objects.aggregate(Sum('read_count'))
    # BookInfo.objects.all().order_by('read_count')
    # BookInfo.objects.all().order_by('-read_count')
    # book = BookInfo.objects.get(id=1)
    # book.authorinfo_set.all()
    # AuthorInfo.objects.get(id=2).book.comment_count
    # BookInfo.objects.filter(authorinfo__name='郭靖')
    # BookInfo.objects.filter(authorinfo__description__contains='八')
    # AuthorInfo.objects.filter(book__name='天龙八部')
    # AuthorInfo.objects.filter(book__read_count__gt=30)
    pass


def create_book(request):
    BookInfo.objects.create(
        name='abcd',
        pub_date='2020-1-1',
        read_count=10,
    )
    return HttpResponse('create book')


def search_book(request):
    query_set = request.GET.get('age')
    print(query_set)
    return HttpResponse('search book')


def register(request):
    data = request.POST
    print(data)
    return HttpResponse('register success')


def json_format(request):
    body = request.body
    body_str = body.decode('utf-8')
    body_dict = json.loads(body_str)
    # print(body_dict)
    # print(type(body_dict))
    # print(request.META)
    print(request.META['SERVER_PORT'])
    return HttpResponse('json')


def response_test(reqeust):
    info = {
        "name": "ryan",
        "age": 23,
        "gender": "Male"
    }
    girl_friends = [
        {
            "name": "Lily",
            "age": 23,
            "gender": "Female"
        },
        {
            "name": "Rose",
            "age": 23,
            "gender": "Female"
        }
    ]
    response = JsonResponse(girl_friends, safe=False)
    print(type(response))
    return HttpResponse(content="response test", status=200)


